"""
主题数据模型
"""
from sqlalchemy import Column, String, Text, Boolean, Integer, DateTime, JSON
from sqlalchemy.sql import func
from app.database import Base
import uuid


class Theme(Base):
    """主题模型"""
    
    __tablename__ = "themes"
    
    id = Column(String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
    name = Column(String(255), nullable=False, comment="主题名称")
    description = Column(Text, comment="主题描述")
    keywords = Column(JSON, comment="关键词数组")
    style = Column(String(100), comment="写作风格")
    target_audience = Column(String(100), comment="目标受众")
    content_length = Column(Integer, default=1500, comment="内容长度要求")
    is_active = Column(Boolean, default=True, comment="是否启用")
    created_at = Column(DateTime(timezone=True), server_default=func.now(), comment="创建时间")
    updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now(), comment="更新时间")
    
    def __repr__(self):
        return f"<Theme(id={self.id}, name={self.name})>"
    
    def to_dict(self):
        """转换为字典"""
        return {
            "id": self.id,
            "name": self.name,
            "description": self.description,
            "keywords": self.keywords,
            "style": self.style,
            "target_audience": self.target_audience,
            "content_length": self.content_length,
            "is_active": self.is_active,
            "created_at": self.created_at.isoformat() if self.created_at else None,
            "updated_at": self.updated_at.isoformat() if self.updated_at else None,
        }